package com.molyfun.parents.activity.msg.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.classroom.ClassRoomActivity;
import com.molyfun.parents.activity.classroom.SingleChatActivity;
import com.molyfun.parents.dao.entity.SingleChatDB;
import com.molyfun.parents.utils.StringUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 *
 */
public class SingleMsgListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<SingleChatDB> mList;
    private SingleMsgClassRoomClickCallback mSingleMsgClassRoomClickCallback;

    public SingleMsgListAdapter(Context context, SingleMsgClassRoomClickCallback singleMsgClassRoomClickCallback) {
        this.mContext = context;
        mSingleMsgClassRoomClickCallback = singleMsgClassRoomClickCallback;
    }

    private String TAG = "MsgListAdapter";

    public void refreshData(List<SingleChatDB> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void addMsgCount(String conversationid){
        if(mList != null){
            for(SingleChatDB singleChatDB : mList){
                if(singleChatDB.getConversationid().equals(conversationid)){
                    singleChatDB.setUnReadMsgCount(singleChatDB.getUnReadMsgCount() + 1);
                }
            }
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if(mList != null && mList.size() > 0){
            mList.remove(position);
            notifyDataSetChanged();
        }
    }

    public void clearAll() {
        if (mList != null) {
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<SingleChatDB> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SingleMsgViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_single_msg_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        SingleMsgViewHolder singleMsgViewHolder = (SingleMsgViewHolder) holder;
        SingleChatDB singleChatDB = mList.get(position);
        if(!TextUtils.isEmpty(singleChatDB.getCover())){
            Picasso.with(mContext)
                    .load(singleChatDB.getCover())
                    .placeholder(R.mipmap.ic_header_default)
                    .into(singleMsgViewHolder.mCircleImageView);
        }else{
            singleMsgViewHolder.mCircleImageView.setImageResource(R.mipmap.ic_header_default);
        }

        if(singleChatDB.getUnReadMsgCount() > 0){
            singleMsgViewHolder.mMsgCount.setVisibility(View.VISIBLE);
            singleMsgViewHolder.mMsgCount.setText(singleChatDB.getUnReadMsgCount() + "");
        }else{
            singleMsgViewHolder.mMsgCount.setVisibility(View.GONE);
        }

        singleMsgViewHolder.mName.setText(singleChatDB.getName());
        if(singleChatDB.getIsTeam()){
            singleMsgViewHolder.mTeamText.setVisibility(View.VISIBLE);
            if(TextUtils.isEmpty(singleChatDB.getType()) || singleChatDB.getType().equals("group")){
                singleMsgViewHolder.mTeamText.setText("小组");
            }else{
                singleMsgViewHolder.mTeamText.setText("班级");
            }


        }else{
            singleMsgViewHolder.mTeamText.setVisibility(View.GONE);
        }

        singleMsgViewHolder.mTime.setText(StringUtils.getChatTime(singleChatDB.getLastMsgTime()));
        if(position == (getItemCount() - 1)){
            singleMsgViewHolder.mLine.setVisibility(View.GONE);
        }else{
            singleMsgViewHolder.mLine.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

    public interface SingleMsgClassRoomClickCallback{
        void onClassRoomLongClick(SingleChatDB singleChatDB, int position);
    }

    public class SingleMsgViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

        @InjectView(R.id.avator)
        CircleImageView mCircleImageView;
        @InjectView(R.id.name)
        TextView mName;
        @InjectView(R.id.team_text)
        TextView mTeamText;
        @InjectView(R.id.msg_count)
        TextView mMsgCount;
        @InjectView(R.id.time)
        TextView mTime;
        @InjectView(R.id.line)
        View mLine;

        public SingleMsgViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
            this.itemView.setOnClickListener(this);
            this.itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            SingleChatDB singleChatDB = mList.get(getAdapterPosition());
            if(singleChatDB != null){
                if(singleChatDB.getIsTeam()){
                    //过期用专门的接口获取
                    ClassRoomActivity.startActivity(mContext, singleChatDB.getTeamId(), singleChatDB.getConversationid(), singleChatDB.getTeamMemberCount(), singleChatDB.getName(), singleChatDB.getTeamLiveState(), singleChatDB.getRule(), false, true);
                }else{
                    SingleChatActivity.startActivity(mContext, singleChatDB.getConversationid(), singleChatDB.getUserId(), singleChatDB.getName(), singleChatDB.getTargettype());
                }
                mMsgCount.setVisibility(View.GONE);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            SingleChatDB singleChatDB = mList.get(getAdapterPosition());
            if(mSingleMsgClassRoomClickCallback != null){
                mSingleMsgClassRoomClickCallback.onClassRoomLongClick(singleChatDB, getAdapterPosition());
            }
            return false;
        }
    }

}
